﻿/******************************************************************
** 文件名:Sys_Operator.cs
** Copyright (c) 
** 创建人:佟礼
** 日 期:2012-05-26 15:43:11
** 描 述:
********************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Enter.BizLogic.Base;
using Enter.Common;
using Enter.Entity;
namespace Enter.BizLogic
{
	public  class Sys_OperatorBLLSub:Sys_OperatorBLLBase
	{
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="LoginName"></param>
        /// <param name="LoginPwd"></param>
        /// <param name="sys_user"></param>
        /// <returns></returns>
        public static ResultPacket Login(string LoginName, string LoginPwd, ref Sys_OperatorEntity sys_user)
        {
            ResultPacket result = new ResultPacket();

            sys_user = Manager.GetObjectByLoginName(LoginName);
            if (sys_user == null)
            {
                result.IsError = true;
                result.Description = "用户名或密码错误";
                return result;
            }

            if (sys_user.Password != LoginPwd)
            {
                result.IsError = true;
                result.Description = "用户名或密码错误";
                return result;
            }
            if (sys_user.Status == Sys_OperatorEntity.EnumStatus.NoValid)
            {
                result.IsError = true;
                result.Description = "无效的用户";

                return result;
            }

            result.IsError = false;
            return result;
        }
        public static Sys_OperatorEntity GetObjectByLoginName(string loginName)
        {
           
            return Manager.GetObjectByLoginName(loginName);
        }
        /// <summary>
        /// 根据登录名得到可以操作的菜单
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="merchantId"></param>
        /// <returns></returns>
        public static DataTable GetOperatorMenuList(string loginName)
        {
            if (loginName.ToLower() == "admin") return new Sys_PopMenuListBLLSub().GetValidMenus();

            Sys_OperatorEntity sys_user = GetObjectByLoginName(loginName);
            if (sys_user == null) return null;

            Sys_RoleBLLSub roleRightBll = new Sys_RoleBLLSub();
            if (sys_user.RoleList == "") return null;

            StringBuilder rights = new StringBuilder();
            string[] roleIds = sys_user.RoleList.Split(',');

            for (Int32 i = 0; i < roleIds.Length; i++)
            {
                Sys_RoleEntity roleRights = roleRightBll.GetObjectByID(int.Parse(roleIds[i]));
                if (roleRights == null) continue;
                if (roleRights.Rights == "") continue;

                rights.AppendFormat("{0},", roleRights.Rights);
            }

            if (rights.ToString() == "") return null;

            return new Sys_PopMenuListBLLSub().GetSysMenuValidByMenuNoList(rights.ToString().TrimEnd(','));
        }
	}
}